package com.tanhua.server.impl;

import com.alibaba.fastjson.JSON;
import com.tanhua.commons.utils.Constants;
import com.tanhua.exception.BusinessException;
import com.tanhua.model.domain.ErrorResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Map;

@Service
public class UserFreezeService {
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    //根据操作状态判断权限
    public void checkUserStatus(Integer state, Long userId) {
        String key = Constants.FREEZE_USER + userId;
        String vule = redisTemplate.opsForValue().get(key);
        if (!StringUtils.isEmpty(vule)) {
            Map map = JSON.parseObject(vule, Map.class);
            Integer freezingRange = (Integer) map.get("freezingRange");//1 2  3
            if(state==freezingRange){
                throw new BusinessException(ErrorResult.builder().errMessage("您的账号被冻结！").build());
            }
        }
    }
}
